package lowijs.util.logging;

import static lowijs.XfDroid.XfDroidConstants.LOG_TAG;

import java.io.File;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import lowijs.XfDroid.ErrorHandler;
import lowijs.XfDroid.Runner;
import lowijs.XfireClient.IM;
import lowijs.XfireClient.XfireClient;
import lowijs.XfireClient.XfireStatus;
import lowijs.XfireClient.util.Util;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.util.Log;

public class DeveloperLogging extends Logging {
	private Logger logger;

	public DeveloperLogging() {
		logger = Logger.getLogger("lowijs.XfDroid");
		int meg = 1048576;
		try {
			File file = Util.getFile("logging/xfdroid%g.log");
			file.getParentFile().mkdirs();
			FileHandler handler = new FileHandler(file.getAbsolutePath(), meg * 10, 10);
			handler.setLevel(Level.ALL);
			logger.addHandler(handler);
			logger.setLevel(Level.ALL);
			logger.setUseParentHandlers(false);
			handler.setFormatter(new OneLineFormatter());
		} catch (Exception e) {
			Log.e("DeveloperLogging", "Problem with logging", e);
		}
	}

	@Override
	public void clientBadVersion() {
		debug("Client received a bad version, server reported a new version but our version is newer");
	}

	@Override
	public void clientBeginThread() {
		debug("Client thread has started");
	}

	@Override
	public void clientClose() {
		debug("Client clossing");
	}

	@Override
	public void clientDisconnect() {
		debug("Client disconnecting");
	}

	@Override
	public void clientEnterThread() {
		debug("Client thread starting");
	}

	@Override
	public void clientException(String message, Throwable t) {
		severe(message, t);
	}

	@Override
	public void clientExistingThread() {
		debug("Client thread already running, not starting a new one");
	}

	@Override
	public void clientExitThread() {
		debug("Client thread stopped");
	}

	@Override
	public void clientLogin() {
		debug("Client attempting to login");
	}

	@Override
	public void clientLoginFailed() {
		debug("Client failed to login");
	}

	@Override
	public void clientNewVersion(int version, int oldVersion) {
		debug("Client found a new version: " + oldVersion + " > " + version);
	}

	@Override
	public void clientRecievedPacket(int type) {
		debug("Client recieved new packet " + type);
	}

	@Override
	public void clientUnknownCode(int type) {
		debug("Client recieved an unknown packet " + type + "[0x" + Integer.toHexString(type) + "]");
	}

	@Override
	public void clientBooted() {
		debug("Some one else is using the account on another machine");
	}

	@Override
	public void reconnectorEndThread() {
		debug("Reconnector is stopping");
	}

	@Override
	public void reconnectorNetworkConnected() {
		debug("Current network is connected");
	}

	@Override
	public void reconnectorNetworkDisconnected() {
		debug("Current network is disconnected");
	}

	@Override
	public void reconnectorNoNetwork() {
		debug("No network found");
	}

	@Override
	public void reconnectorReconnected() {
		debug("Reconnected");
	}

	@Override
	public void reconnectorStartThread() {
		debug("Reconnector is starting");
	}

	@Override
	public void reconnectorToManyTries() {
		debug("To many failed attemps, reconnect terminating");
	}

	@Override
	public void serviceHandleEvent(int eventType) {
		debug("Service handling event " + eventType);
	}

	@Override
	public void serviceReconenctFailed() {
		debug("Service found failed reconnect attempt");
	}

	@Override
	public void serviceReconencted() {
		debug("Service found reconnect successful");
	}

	@Override
	public void serviceStopSelf() {
		debug("Service is stopping");
	}

	@Override
	public void serviceStopping(Runnable runner) {
		debug("Service runner has stopped: " + runner);
	}

	@Override
	public void serviceStarting(Runner runner) {
		debug("Service runner has started: " + runner);
	}

	@Override
	public void clientProcess(List<RunningAppProcessInfo> procs) {
		for (RunningAppProcessInfo proc : procs) {
			debug("Proc: " + proc.processName + ": " + proc.importance + ": " + proc.toString());
		}
	}

	@Override
	public void clientTasks(List<RunningTaskInfo> tasks) {
		for (RunningTaskInfo task : tasks) {
			debug("Task: " + task.topActivity.flattenToString() + ": " + task.baseActivity.flattenToString() + ": " + task.description + ": " + task.toString());
		}
	}

	@Override
	public void serviceDC() {
		debug("Service recieved DC signal");
	}

	@Override
	public void serviceLogoff() {
		debug("Service recieved logoff signal");
	}

	private void debug(String message) {
		Log.d(LOG_TAG, message);
		logger.fine(message);
	}

	private void severe(String message, Throwable t) {
		logger.log(Level.SEVERE, message, t);
		Log.e(LOG_TAG, message, t);
		ErrorHandler.write(t);
	}

	@Override
	public void clientReconnected(boolean reconnectStatus) {
		if (reconnectStatus) {
			debug("Reconnect successful!");
		} else {
			debug("Failed to reconnect.");
		}
	}

	@Override
	public void badChannel(IM im) {
		debug("Bad channel: "+ im.getChannelid() + ": " + im.getUser());
	}

	/*
	@Override
	public void clientConnected() {
		debug("Client thread connected");
	}
	 */

	@Override
	public void clientDisconnected() {
		debug("Client thread disconnected");
	}

	@Override
	public void serviceHasNoClientRunner() {
		debug("There is no running client thread");
	}

	@Override
	public void serviceNotRunning() {
		debug("Service is not running");
	}

	@Override
	public void clientCreated(XfireClient xc) {
		debug("Client created [" + xc + "]");
	}

	@Override
	public void serviceCreated() {
		debug("Service created");
	}

	@Override
	public void serviceDestroyed() {
		debug("Service destroyed");
	}

	@Override
	public void serviceStarted() {
		debug("Service started");
	}

	@Override
	public void friendsOnContextItemSelected() {
		debug("Friends On Context Item Selected");
	}

	@Override
	public void friendsOnCreate() {
		debug("Friends On Create");
	}

	@Override
	public void friendsOnCreateOptionsMenu() {
		debug("Friends On Create Options Menu");
	}

	@Override
	public void friendsOnDestroy() {
		debug("Friends On Destroy");
	}

	@Override
	public void friendsOnOptionsItemSelected() {
		debug("Friends On Options Item Selected");
	}

	@Override
	public void friendsOnRestoreInstanceState() {
		debug("Friends On Restore Item Selected");
	}

	@Override
	public void friendsOnSaveInstanceState() {
		debug("Friends On Save Instance State");
	}

	@Override
	public void friendsOnStart() {
		debug("Friends On Start");
	}

	@Override
	public void friendsOnStop() {
		debug("Friends On Stop");
	}

	@Override
	public void friendsShowLogin() {
		debug("Friends Show Login");
	}

	@Override
	public void friendsShowReconnect() {
		debug("Friends Show Reconnect");
	}

	@Override
	public void clientSatusChanged(XfireStatus status) {
		debug("Status changed to " + status.name());
	}

	@Override
	public void lostConnection() {
		debug("Lost connection, show reconnect");
	}

	@Override
	public void loggingOff() {
		debug("Logoff option pressed");
	}

	@Override
	public void disconnectingClient() {
		debug("Disconnecting client");
	}

	@Override
	public void stoppingService() {
		debug("Stopping service");
	}

	@Override
	public void startConnection() {
		debug("Connection starting");
	}

	@Override
	public void connectToXfire() {
		debug("Connecting to xfire server");
	}

	@Override
	public void performLogin() {
		debug("Performing login");
	}

	@Override
	public void clientFinished() {
		debug("Client handle thread finished");
	}

	public void pingFinished() {
		debug("Ping finished");
	}

	public void pingStopped() {
		debug("Ping stopped");
	}

	public void pingFailed(Throwable t) {
		severe("Ping failed", t);
	}
}
